/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","sysmanMain", "bizProto","btnServ","flowHistoric"],
		function($,datagridServ, sysmanMain, bizProto,btnServ,flowHistoric) {

			'use strict';

			var 
				//锚记datagrid目标元素
				medicalInfoWindow='#win-medical-infomation',
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,$edit_window_height=500,$edit_window_width=900,window_btn,
				_buttonConfig,
				//业务表单对象
				flowHistoricUrl='/flowOperator/flowHistorics.action',	
				medicalInfoUrl='/medicalInfo/query/mainPage.action',
				_selectedRecord,
				rootId='panel-medical-info'
			var public_method = {				
				pageOpen:function(_options){
					var _defaultConfig = {
						rootId:rootId,
						subSystemCode:'medical',
						moduleCode:'medicalInfo',						
						initQueryParams:null,
						queryModule:null,
						queryMethod:null,
						width:900,
						height:700
					}
					
					
					//获取参数配置信息
					$.extend(_defaultConfig,_options)
					
					
					
					var rootPath = '/'+sysmanMain.getRootPath();
					var _config = sysmanMain.getModuConf('medicalInfo')
					if(!_config){
						_config = {}
					}
					
					$.extend(_config,_defaultConfig)
					sysmanMain.setModuConf('medicalInfo',_config)
					var _param = {
						window:$(medicalInfoWindow),
						href:rootPath + medicalInfoUrl,
						width:_defaultConfig.width,
						height:_defaultConfig.height,
						onClose:private_method.onClose,
						flag:false}
					datagridServ.windowEdit(_param);	
				},
				
				clickRow:function(index,data){
					if(window.console){
						console.info('clicked index: ' + index)
					}
					var $grid = $('#panel-medical-info table.dg-list')
					$($grid).datagrid('clearSelections')
					$($grid).datagrid('selectRow',index)
					var rootPath = '/'+sysmanMain.getRootPath();
					
					_buttonConfig={
						"返回":public_method.changePanel,
					    "查看详情":public_method.detailInfo
					}
					var btn = $('#panel-medical-info div.form-panel a.function-btn')
					
					$(btn).unbind('click')
					$(btn).bind('click',public_method.function_btn_listenner)
					
					var $form = '#panel-medical-info div.form-panel form'
					_selectedRecord = $($grid).datagrid('getSelected')
					$($form).form('load',_selectedRecord)
					
					var $gridHistoric = $('#panel-medical-info div.historics')
					
					flowHistoric.initInternalGrid({historicId:$gridHistoric,record:_selectedRecord})
						
					
					public_method.changePanel()
				},
				// 功能按钮监听函数
				function_btn_listenner : function(e) {
		
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
		
				},
				changePanel:function(e){
					
					var $listPanel = $('#panel-medical-info div.list-panel' )
					var $formPanel = $('#panel-medical-info div.form-panel' )
					
					public_method.togglePanel({target:$listPanel})
					public_method.togglePanel({target:$formPanel})
					if(e && e.currentTarget){
						$(e.currentTarget).linkbutton('enable');
					}
				},
				detailInfo:function(e){
					var _patient_type = _selectedRecord.patient_type
					var _config = {initQueryParams:{}}
					switch(_patient_type){
					case "1":
						//门诊
						var outpatientElement = $('#win-medical-infomation form #patient_id')
						
						var _patient_id
						_patient_id = _selectedRecord.patient_id
						
						//判断处方号是否为空
						if(_patient_id){
							_config.initQueryParams = {condition:{outpatient_number:_patient_id, prescribe_code_like:'cf'}}
							sysmanMain.setModuConf('outpatientItemFee',_config)
							sysmanMain.medicalOutpatientItemFeeSelect()
						}
						break;
					case "2":
						
						//获取患者编号
						var inpatientElement = $('#win-medical-infomation form #patient_id')
						
						var _inpatientno
						_inpatientno = _selectedRecord.patient_id
						
						//判断住院号是否为空
						if(_inpatientno){
							_config.initQueryParams = {condition:{patient_id:_inpatientno}}
							sysmanMain.setModuConf('inpatientItemFee',_config)
							sysmanMain.medicalInpatientItemFeeSelect()
						}
						//住院
						
						break;
					default:
						
						//获取患者编号
						var inpatientElement = $('#win-medical-infomation form #patient_id')
						
						var _inpatientno
						_inpatientno = _selectedRecord.patient_id
						
						//判断住院号是否为空
						if(_inpatientno){
							_config.initQueryParams = {condition:{patient_id:_inpatientno}}
							sysmanMain.setModuConf('inpatientItemFee',_config)
							sysmanMain.medicalInpatientItemFeeSelect()
						}
					}
					
					if(e && e.currentTarget){
						$(e.currentTarget).linkbutton('enable');
					}
//					var _config={}
//					_config.initQueryParams = {field:'patient_id',fieldVal:_selectedRecord.patient_id}
//					sysmanMain.setModuConf('inpatientItemFee',_config)
//					sysmanMain.medicalInpatientItemFeeSelect()
				},
				/**
				 * 打开或关闭详情页面
				 * @param _options.target
				 */
				
				togglePanel : function(_options) {
					var _queryParam = {}
					//获取当前模块配置
					var _target
					
					if(_options && _options.target && _options.target.length>0){
						_target= _options.target
						if(_target.length>0){
							
							var _tmp,isClosed
							var _tmpPanel = $(_target).closest('div.panel')
							isClosed=$(_tmpPanel).attr('style')					
							 
							if(isClosed){
								isClosed = isClosed.toLowerCase();
								_tmp = isClosed.indexOf('display: none')
								if(_tmp<0){
									_target.panel('close')
								}else{
									_target.panel('open')
								}
							}
								
							
						}
					}
				}

			};

			var private_method ={
				/**
				 * 更新historic列表组件
				 */
				fillHistoricInfo:function(){
					
				},
				onClose:function(){
					
					var btn = $('#win-medical-infomation div.form-panel a.function-btn')
					$(btn).unbind('click')
					
					var _config = sysmanMain.getModuConf('medicalInfo')
					if(_config){
						_config = {}
					}					
					sysmanMain.setModuConf('medicalInfo',_config)
					$(medicalInfoWindow).empty()
				}
			}
			return public_method;

		});